package org.jc.subject.domain.handler.subject;

import com.google.common.base.Preconditions;
import jakarta.annotation.Resource;
import org.jc.subject.common.enums.IsDeletedFlagEnum;
import org.jc.subject.common.enums.SubjectInfoTypeEnum;
import org.jc.subject.domain.entity.SubjectInfoBO;
import org.jc.subject.domain.entity.SubjectOptionBO;
import org.jc.subject.infra.basic.entity.SubjectMultiple;
import org.jc.subject.infra.basic.entity.SubjectRadio;
import org.jc.subject.infra.basic.service.SubjectRadioService;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * 功能描述
 *
 * @author wanghongqian
 * @date 2024/06/24 23:00
 */
@Component
public class RadioTypeHandler implements SubjectTypeHandler{

    @Resource
    private SubjectRadioService subjectRadioService;

    @Override
    public SubjectInfoTypeEnum getHandlerType() {
        return SubjectInfoTypeEnum.RADIO;
    }

    @Override
    public void add(SubjectInfoBO subjectInfoBO) {
        List<SubjectRadio> subjectRadioList = new LinkedList<>();

        Preconditions.checkArgument(!CollectionUtils.isEmpty(subjectInfoBO.getOptionList()),"选项不能为空");

        subjectInfoBO.getOptionList().forEach(option -> {
            SubjectRadio subjectRadio = new SubjectRadio();
            subjectRadio.setSubjectId(subjectInfoBO.getId());
            subjectRadio.setOptionType(option.getOptionType());
            subjectRadio.setOptionContent(option.getOptionContent());
            subjectRadio.setIsCorrect(option.getIsCorrect());
            subjectRadio.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
            subjectRadioList.add(subjectRadio);
        });
        subjectRadioService.insertBatch(subjectRadioList);
    }

    @Override
    public SubjectOptionBO query(Long subjectId) {
        SubjectRadio condition = new SubjectRadio();
        condition.setSubjectId(subjectId);
        List<SubjectRadio> subjectMultiples = subjectRadioService.queryByCondition(condition);
        List<SubjectInfoBO.SubjectAnswerBO> answerBOList = new ArrayList<>();
        subjectMultiples.forEach(o -> {
            SubjectInfoBO.SubjectAnswerBO bo = new SubjectInfoBO.SubjectAnswerBO();
            bo.setOptionType(o.getOptionType());
            bo.setOptionContent(o.getOptionContent());
            bo.setIsCorrect(o.getIsCorrect());
            answerBOList.add(bo);
        });

        SubjectOptionBO bo = new SubjectOptionBO();
        bo.setOptionList(answerBOList);
        return bo;
    }
}
